package com.cloud.phone.utils

import android.content.Context
import android.os.Bundle
import com.cloud.phone.constants.Constants
import com.uc.crashsdk.export.CrashApi
import com.umeng.analytics.MobclickAgent
import com.umeng.commonsdk.UMConfigure
import com.umeng.umcrash.IUMCrashCallbackWithType.CrashType
import com.umeng.umcrash.UMCrash


/**
 * @auth: njb
 * @date: 2024/6/15 3:50
 * @desc: 描述
 */
object UMengUtils {
    /**
     * 友盟初始化
     */
    fun init(context: Context?) {
        // 集成Umeng崩溃
        val bundle = Bundle()
        bundle.putBoolean(UMCrash.KEY_ENABLE_CRASH_JAVA, true)
        bundle.putBoolean(UMCrash.KEY_ENABLE_CRASH_NATIVE, true)
        bundle.putBoolean(UMCrash.KEY_ENABLE_CRASH_UNEXP, true)
        bundle.putBoolean(UMCrash.KEY_ENABLE_ANR, false)
        bundle.putBoolean(UMCrash.KEY_ENABLE_PA, false)
        bundle.putBoolean(UMCrash.KEY_ENABLE_LAUNCH, false)
        bundle.putBoolean(UMCrash.KEY_ENABLE_MEM, false)
        bundle.putBoolean("UMCrash.KEY_ENABLE_NET",true)
        bundle.putLong(UMCrash.KEY_PA_TIMEOUT_TIME, 2000L)//设置卡顿阈值为2000L
        UMCrash.initConfig(bundle)
        // 设置友盟log开关,默认为false
        UMConfigure.setLogEnabled(true)
        // 初始化组件化基础库, 统计SDK
        UMConfigure.init(
            context,
            Constants.UM_APP_KEY,
            Constants.UM_APP_CHANNEL,
            UMConfigure.DEVICE_TYPE_PHONE,
            null
        )
        // 设置统计模式
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO)
        // 设置发送策略时间为40秒
        MobclickAgent.setSessionContinueMillis((1000 * 40).toLong())
        UMCrash.registerUMCrashCallback { type ->
            when (type) {
                CrashType.CRASH_TYPE_NATIVE -> "Native crash"
                CrashType.CRASH_TYPE_JAVA -> "JAVA crash"
                CrashType.CRASH_TYPE_ANR -> "ANR crash"
                CrashType.CRASH_TYPE_CUSTOM_LOG -> "custom log"
                CrashType.CRASH_TYPE_BLOCK -> "卡顿 log"
                else -> null
            }
        }
        val customInfo = Bundle()
        customInfo.putBoolean("mCallNativeDefaultHandler",true)
        CrashApi.getInstance().updateCustomInfo(customInfo)
        UMCrash.addCustomInfo("cloud-phone","crash")
    }

}